package com.jckl.foodathon.sqlite.services;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.jckl.foodathon.common.constant.ApplicationConstants;
import com.jckl.foodathon.sqlite.dao.DaoMaster;
import com.jckl.foodathon.sqlite.dao.DaoSession;

public class SqliteCommon {
	
//	public boolean checkDatabaseExist() {
//		SQLiteDatabase checkDB = null;
//		try{
//			checkDB = SQLiteDatabase.openDatabase(ApplicationConstants.DATABASE_NAME, null, SQLiteDatabase.OPEN_READONLY);
//			checkDB.close();
//		} catch (SQLiteException SQLEx){
//			Log.e(CommunicationConstants.APP_TAG, "Database not exist");
//			SQLEx.printStackTrace();
//		}
//		return checkDB != null ? true : false;
//	}
//	
//	public void createDatabase(Context context){
//		new DaoMaster.DevOpenHelper(context, ApplicationConstants.DATABASE_NAME, null);
//	}
	
	public DaoSession setupDatabase(Context context){
		DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, ApplicationConstants.DATABASE_NAME, null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        
        return daoSession;
	}
	
	public void resetDatabase(Context context){
		DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, ApplicationConstants.DATABASE_NAME, null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster.dropAllTables(db, true);
        DaoMaster.createAllTables(db, true);
	}

}
